package com.detrum.utils;

import com.detrum.base.BaseStatic;

/**
 * Created by abc on 2018/5/16.
 */

public class SPSParse {
    private static SPSParse spsParse;
    private String allString;
    private String sizeString;

    private  SPSParse(byte[] sps) {
        allString = getBitList(sps);
        getSizeString();
    }

    public static SPSParse getInstence(byte[] sps){
        if(spsParse==null){
            synchronized (SPSParse.class){
                spsParse = new SPSParse(sps);
            }
        }
        return spsParse;
    }

    public int getLevel_ide(byte[] sps){
        String str1 = allString.substring(24);
        String str2 = str1.substring(0,8);
        return getInt(str2);
    }

    private void getSizeString(){
        if(allString!=null&&allString.length()!=0){
            String str1 = allString.substring(32);
            int seq_parameter_set_id = getNewIntUe(str1);
            BaseStatic.savaInfoToSD("seq_parameter_set_id = "+seq_parameter_set_id);
            String str2 = getNewStrUe(str1);
            int log2_max_frame_num_minus4 = getNewIntUe(str2);
            BaseStatic.savaInfoToSD("log2_max_frame_num_minus4 = "+log2_max_frame_num_minus4);
            String str3 = getNewStrUe(str2);
            int pic_order_cnt_type = getNewIntUe(str3);
            BaseStatic.savaInfoToSD("pic_order_cnt_type = "+pic_order_cnt_type);
            String str4 = getNewStrUe(str3);
            if(pic_order_cnt_type==0){

            }else if(pic_order_cnt_type==1){

            }else{
                int num_ref_frames = getNewIntUe(str4);
                BaseStatic.savaInfoToSD("num_ref_frames = "+num_ref_frames);
                String str5 = getNewStrUe(str4);
                int gaps_in_frame_num_value_allowed_flag = Integer.parseInt(str5.substring(0,1));
                BaseStatic.savaInfoToSD("gaps_in_frame_num_value_allowed_flag = "+gaps_in_frame_num_value_allowed_flag);
                sizeString = str5.substring(1);
            }
        }
    }

    public int[] getSize(){
        int[] size = new int[2];
        if(sizeString!=null&&sizeString.length()!=0){
            size[0]=(getNewIntUe(sizeString)+1)*16;
            String str = getNewStrUe(sizeString);
            size[1]=(getNewIntUe(str)+1)*16;
        }
        return size;
    }

    private int getInt(String str){
        int xx = 0;
        for (int i = 0; i < str.length(); i++) {
            xx += Integer.parseInt(str.substring(i, i+1))*Math.pow(2, str.length()-i-1);
        }
        return xx;
    }

    private String getBitList(byte[] sps){
        StringBuffer stringBuffer = new StringBuffer();
        for (int i = 0; i < sps.length; i++) {
            int xx = sps[i]&0xff;
            stringBuffer.append(getStr(xx));
        }
        return stringBuffer.toString();
    }

    private String getStr(int xx){
        StringBuffer sBuffer = new StringBuffer();
        for (int i = 128; i >= 1; i/=2) {
            sBuffer.append(xx/i);
            xx = xx%i;
        }
        return sBuffer.toString();
    }

    private String getNewStrUe(String strs){
        String str=null;
        for (int i = 0; i < strs.length(); i++) {
            if(Integer.parseInt(strs.substring(i, i+1))==1){
                if(i!=0){
                    str = strs.substring(2*i+1);
                }else{
                    str = strs.substring(1);
                }
                break;
            }
        }
        return str;
    }
    private int getNewIntUe(String strs){
        int result=0;
        for (int i = 0; i < strs.length(); i++) {
            if(Integer.parseInt(strs.substring(i, i+1))==1){
                if(i!=0){
                    String str4 = strs.substring(i,2*i+1);
                    result = getInt(str4)-1;
                }else{
                    result = 1;
                }
                break;
            }
        }
        return result;
    }
    private String getNewStrSe(String strs){
        String str=null;
        for (int i = 0; i < strs.length(); i++) {
            if(Integer.parseInt(strs.substring(i, i+1))==1){
                if(i!=0){
                    str = strs.substring(2*i+1);
                }else{
                    str = strs.substring(2);
                }
                break;
            }
        }
        return str;
    }
    private int getNewIntSe(String strs){
        int result=0;
        for (int i = 0; i < strs.length(); i++) {
            if(Integer.parseInt(strs.substring(i, i+1))==1){
                if(i!=0){
                    String str4 = strs.substring(i,2*i);
                    int ret = Integer.parseInt(strs.substring(2*i,2*i+1));
                    if(ret==1){
                        result = -getInt(str4);
                    }else{
                        result = getInt(str4);
                    }
                }else{
                    int ret = Integer.parseInt(strs.substring(1,2));
                    if(ret==1){
                        result = -1;
                    }else{
                        result = 1;
                    }
                }
                break;
            }
        }
        return result;
    }
}
